package com.cloudcc.mobile.adapter;

import java.util.List;

import android.content.Context;
import android.support.v4.widget.DrawerLayout.LayoutParams;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.entity.AddImageInfo;
import com.cloudcc.mobile.util.photo.Constants;
import com.cloudcc.mobile.util.photo.Util;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

public class AddmicroPostGridviewAdapter extends BaseAdapter {
	private Context context;
	private List<AddImageInfo> list;
	private boolean isInDeleteMode = false;
	private OndeleteListener ondeleteListener;
	private OnAddImageListener onAddImageListener;
	private OnShowImageListener onShowImageListener;
	private ImageLoadingListener animateFirstListener = new Util.AnimateFirstDisplayListener();

	public AddmicroPostGridviewAdapter(Context context, List<AddImageInfo> list) {
		this.context = context;
		this.list = list;
	}

	public void changeData(List<AddImageInfo> list) {
		this.list = list;
		notifyDataSetChanged();
	}

	public void setDeleteModel(boolean isInDeleteMode) {
		this.isInDeleteMode = isInDeleteMode;
		this.notifyDataSetChanged();
	}

	// 设置事件监听
	public void setOndeleteListener(OndeleteListener ondeleteListener) {
		this.ondeleteListener = ondeleteListener;
	}

	public void setOnAddImageListener(OnAddImageListener onAddImageListener) {
		this.onAddImageListener = onAddImageListener;
	}

	public void setOnShowImageListener(OnShowImageListener onShowImageListener) {
		this.onShowImageListener = onShowImageListener;
	}

	@Override
	public int getCount() {
		return 2 + list.size();
	}

	@Override
	public AddImageInfo getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		// TODO Auto-generated method stub
		if (convertView == null)
			convertView = LayoutInflater.from(context).inflate(
					R.layout.image_grid_item, null);
		ImageView image = (ImageView) convertView
				.findViewById(R.id.image_grid_item_img);
		ImageView delete = (ImageView) convertView
				.findViewById(R.id.badge_delete);
		FrameLayout photo = (FrameLayout) convertView
				.findViewById(R.id.addphoto);

		// 设置滑动图片的宽与高 这里的RelativeLayout是这个布局的父控件是什么布局
		LinearLayout.LayoutParams imagebtn_params = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		imagebtn_params.height = AppContext.screenW / 4 - 15;
		imagebtn_params.width = AppContext.screenW / 4 - 15;
		photo.setLayoutParams(imagebtn_params);
		if (this.isInDeleteMode)
			delete.setVisibility(View.VISIBLE);
		else
			delete.setVisibility(View.GONE);
		delete.setOnClickListener(new View.OnClickListener() {
			public void onClick(View paramAnonymousView) {
				ondeleteListener.delete(position);
			}
		});
		if (position == -1 + getCount()) {
			image.setImageResource(R.drawable.smiley_minus_btn);
			if ((list == null) || list.size() == 0 || isInDeleteMode == true)
				convertView.setVisibility(View.GONE);
			else
				convertView.setVisibility(View.VISIBLE);
			image.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View arg0) {
					isInDeleteMode = true;
					AddmicroPostGridviewAdapter.this.notifyDataSetChanged();
				}
			});
		} else if (position == -2 + getCount()) {
			image.setImageResource(R.drawable.smiley_add_btn);
			delete.setVisibility(View.GONE);
			if (isInDeleteMode == true)
				convertView.setVisibility(View.GONE);
			else
				convertView.setVisibility(View.VISIBLE);
			image.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					isInDeleteMode = false;
					AddmicroPostGridviewAdapter.this.notifyDataSetChanged();
					onAddImageListener.add();

				}
			});
		} else {
			AddImageInfo localImageInfo = list.get(position);
			// image.setImageBitmap(ImageUtils.getimage(localImageInfo.getImagePath()));
			/**
			 * 第一种
			 */
			// image.setImageBitmap(ImageUtils.zoomBitmap(ImageUtils.getimage(localImageInfo.getImagePath()),
			// 50));
			/**
			 * 第二种
			 */
			// 利用Bitmap位图对象生成缩略图
			// Bitmap bitmap =
			// ImageUtils.getImageThumbnail(localImageInfo.getImagePath(), 50,
			// 50);
			// image.setImageBitmap(bitmap);

			// Bitmap
			// bitmap=ImageUtils.getImg(localImageInfo.getImagePath(),50f,50f);
			// byte[] imagebyte=ImageUtils.compressBitmap(bitmap,100f);
			/**
			 * 第三种
			 */
			ImageLoader.getInstance().displayImage(
					"file://" + localImageInfo.getImagePath(), image,
					Constants.qiandao_image, animateFirstListener);
			// /**
			// * 第四种
			// */
			// BitmapUtils bitmapUtils = new BitmapUtils(context);
			// // 加载本地图片(路径以/开头， 绝对路径)
			// bitmapUtils.display(image, localImageInfo.getImagePath());
			image.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					onShowImageListener.show(position);
				}
			});
		}
		return convertView;
	}

	public interface OndeleteListener {
		public void delete(int posion);
	}

	public interface OnAddImageListener {
		public void add();
	}

	public interface OnShowImageListener {
		public void show(int posion);
	}
}
